package cn.haoxsf.app.system.service.impl;

import cn.haoxsf.app.common.util.ToolUtil;
import cn.haoxsf.app.system.entity.Banner;
import cn.haoxsf.app.system.entity.FileInfo;
import cn.haoxsf.app.system.mapper.BannerMapper;
import cn.haoxsf.app.system.service.IBannerService;
import cn.haoxsf.app.system.service.IFileInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author Ele  qvbanfm@qq.com
 * @since 2020-03-13
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 按照类型查询
     *
     * @param banner
     * @return
     */
    @Override
    public List<Map<String, Object>> getList(String banner) {
        List<Map<String, Object>> mapList = this.baseMapper.selectMaps(new QueryWrapper<Banner>().eq("banner_type", banner));
        if (ToolUtil.isNotEmpty(mapList)) {
            List<String> imageId = new ArrayList<>();
            for (Map<String, Object> map : mapList) {
                imageId.add(map.get("imageId").toString());
            }
            String str = ToolUtil.getStr(imageId);
            List<Map<String,Object>> fileInfos = fileInfoService.selectBatchFinalNames(str.split(","));
            for (Map<String, Object> map : mapList) {//遍历广告
                //遍历文件
                for (Map<String,Object> fileInfo : fileInfos) {
                    //是否是同一个文件
                    if(map.get("imageId").equals(fileInfo.get("finalName"))){
                        map.put("imageUrl",fileInfo.get("url"));
                    }
                }
            }
            //删除无用字段
            for (Map<String, Object> map : mapList){
                map.remove("uploadUserId");
                map.remove("createTime");
                map.remove("imageId");
            }
        }
        return mapList;
    }
}
